<template>
	<view class="page-body" style="background-color: background: #EEEEEE;height: 100vh;">
		<Loading class="loading-box" v-show="!isLoading" :isStatus='loadStatus'></Loading>
		<cu-custom bgColor="" :isBack="true">
			<block slot="backText" class="titilestyle">{{title}}</block>

		</cu-custom>

		<view class="oi-detail">
			<view class="oi-header">
				<view class="py-box"><text>{{orderInfo.shopInfo.shop_name}}</text></view>
				<view class="money-boix">
					￥<text class="pay-money">{{orderInfo.online_amount || 0}}</text>
				</view>
				<view class="py-box" @click="orderinfo(orderInfo.order_id)">
					<text>订单编号：{{orderInfo.order_sn||'--'}}</text>
					<uni-icons type="forward" color="#000000" size="10"></uni-icons>
				</view>
			</view>
			<view class="os-pay">
				<view class="os-pay-item middle">
					<text class="tit">支付方式</text>
					<view class="pay-code" @click="selectPay">
						<text v-if="orderInfo.pay_code=='wxpay' || orderInfo.pay_code=='wxgzh'">微信</text>
						<text v-else-if="orderInfo.pay_code=='alipayMobile' || orderInfo.pay_code=='alipay'">支付宝</text>
						<text v-else>其他</text>
						<uni-icons type="forward" color="#000000" size="10"></uni-icons>
					</view>
				</view>
			</view>
			<view class="pay-btn middle" @click="getOrderInfo"><text>立即支付</text></view>
			<view class="v-block"></view>
			<uni-popup ref="pay" type="center">
				<view class="os-pay-list">
					<radio-group @change="checkPay">
						<!-- #ifdef H5 -->
						<view class="os-pay-item">
							<text>微信</text>
							<label class="radio">
								<radio value="wxgzh" name="paycode" :checked="orderInfo.pay_code == 'wxgzh'" />
							</label>
						</view>
						<!-- <view class="os-pay-item">
						<text>支付宝</text>
						<label class="radio"><radio value="alipayMobile" name="paycode" :checked="orderInfo.pay_code == 'alipayMobile'" /></label>
					</view> -->
						<!-- #endif -->
						<!-- #ifdef MP-WEIXIN -->
						<view class="os-pay-item">
							<text>微信</text>
							<label class="radio">
								<radio value="wxpay" name="paycode" :checked="orderInfo.pay_code == 'wxpay'" />
							</label>
						</view>
						<!-- #endif -->
						<!-- #ifdef APP-PLUS -->
						<view class="os-pay-item">
							<text>微信</text>
							<label class="radio">
								<radio value="wxpay" name="paycode" :checked="orderInfo.pay_code == 'wxpayApp'" />
							</label>
						</view>
						<view class="os-pay-item">
							<text>支付宝</text>
							<label class="radio">
								<radio value="alipay" name="paycode" :checked="orderInfo.pay_code == 'alipay'" />
							</label>
						</view>
						<!-- #endif -->
					</radio-group>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import uniPopup from "@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
	export default {
		components: {
			uniPopup
		},
		data() {
			return {
				title: "支付",
				order_id: 0,
				payname: '微信',
				paycode: 'wxpay',
				orderInfo: {
					shop: {},
					order_bill: {},
					pay_code: 'wxpay'
				},
				orderPay: '',
				wxconfig: null,
				isLoading: false, //是否加载完成显示
				loadStatus: 'load',
			}
		},
		filters: {
			numFilter(value) {
				return parseFloat(value)
			}
		},
		onLoad(option) {
			this.order_id = option.order_id
			this.apiRequest('/buyer/pOrderInfo', {
				order_id: this.order_id,
			}).then(res => {

				if (res.code != 200) {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2000);
					return false;
				} else {
					this.orderInfo = res.data
					this.orderInfo.pay_code = this.paycode;
					this.isLoading = true;
				}


			})
		},
		onShow() {
			//#ifdef H5
			if (this.isWeiXin()) {
				/**
				 * 2021 0515 1605 修改数据请求
				 */
				uni.showLoading({
					title: "加载中..."
				})
				this.apiRequest('/mobile/index/getWxconfig', {
					url: window.location.href
				}).then(res => {
					uni.hideLoading();
					if (res.code == 200) {
						this.wxconfig = res.data;

					} else {
						uni.showModal({
							title: "提示",
							content: res.msg,
							showCancel: false,
							success: (e) => {
								if (e.confirm) {
									uni.navigateBack({
										delta: 1
									})
								}
							}

						})
					}


				})

			}
			//#endif
		},
		methods: {
			selectPay() {
				this.$refs.pay.open()
			},
			checkPay(e) {
				this.paycode = e.target.value
				this.orderInfo.pay_code = e.target.value
				this.$refs.pay.close();
			},
			orderinfo(id) {
				uni.navigateTo({
					url: '/app/CloudPrint/pages/order/orderInfo?order_id=' + id
				});
			},
			getOrderInfo() {

				this.apiRequest('/buyer/addPrOrder', this.orderInfo, 1).then(res => {

					if (res.code != 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						return
					}
					this.orderPay = res.data
					this.requestPay('wxpay')



				})

			},
			requestPay(type = 'wxpay') { //默认微信公众号
				var that = this
				//#ifdef H5
				if (type == 'wxgzh') {
					var jweixin = require('jweixin-module')
					jweixin.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: this.wxconfig.appId, // 必填，公众号的唯一标识
						timestamp: this.wxconfig.timestamp, // 必填，生成签名的时间戳
						nonceStr: this.wxconfig.nonceStr, // 必填，生成签名的随机串
						signature: this.wxconfig.signature, // 必填，签名，见附录1
						jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					});

					var payInfo = this.orderPay
					jweixin.ready(function() {
						jweixin.chooseWXPay({
							timestamp: payInfo.timeStamp,
							nonceStr: payInfo.nonceStr,
							package: payInfo.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
							signType: payInfo.signType,
							paySign: payInfo.paySign, // 支付签名
							success: function(res) {
								uni.redirectTo({
									url: '/app/TakeOut/pages/order/orderInfo?order_id=' + that
										.order_id
								});
							},
							cancel: function(res) {
								uni.redirectTo({
									url: '/app/TakeOut/pages/order/orderInfo?order_id=' + that
										.order_id
								});
							},
							fail: function(res) {
								uni.redirectTo({
									url: '/app/TakeOut/pages/order/orderInfo?order_id=' + that
										.order_id
								});
							}
						});
					});

					jweixin.error(function(res) {
						uni.redirectTo({
							url: '/app/TakeOut/pages/order/orderInfo?order_id=' + that.order_id
						});
					})
				}
				//#endif

				//#ifndef H5
				//支付宝支付
				//微信支付
				if (type == 'wxpay') {
					uni.requestPayment({ //订单提交成功，发起支付
						provider: 'wxpay',
						appId: this.orderPay.appId,
						timeStamp: String(this.orderPay.timeStamp),
						nonceStr: this.orderPay.nonceStr,
						package: this.orderPay.package,
						signType: this.orderPay.signType,
						paySign: this.orderPay.paySign,
						success: (res) => {
							this.subscribe('order').then(r => {
								uni.redirectTo({
									url: '/app/CloudPrint/pages/order/orderInfo?order_id=' +
										that.order_id
								});
							})

						},
						fail: function(err) {
							console.log('fail:' + JSON.stringify(err));
							uni.showToast({
								icon: "none",
								title: "取消支付或支付失败",
							})
						}
					});
				}
				//#endif
			},
			isWeiXin() { //判断是否微信浏览器
				var ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			}
		},
	}
</script>

<style scoped>
	* {
		line-height: 1.5;
	}

	/* orderInfo */
	.oi-detail {
		width: 94%;
		margin-left: 3%;
		/* background: #EEEEEE; */
		height: 100%;
		margin-top: 10upx;
	}

	.oi-header,
	.oi-deliver,
	.oi-goods,
	.oi-fee,
	.oi-total,
	.oi-orther-detail {
		background: #FFFFFF;
		padding: 30rpx 5%;
		margin-bottom: 15rpx;
		border-radius: 20rpx;

	}

	.oi-header view {
		line-height: 1.5;
	}

	.oi-header view,
	.oi-deliver view,
	.oi-goods view,
	.oi-fee view,
	.oi-totalview view,
	.oi-orther-detail view {
		background-color: #FFFFFF;
	}

	.oi-header .oi-order-status {
		font-size: 32rpx;
		font-weight: bold;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.oi-header .oi-thank {
		color: #666666;
		font-size: 24rpx;
	}

	.oi-thank {
		padding: 5upx 0 5upx 0;
	}

	.oi-header .oi-order-btn {
		margin-top: 15rpx;
	}

	.oi-header .oi-order-btn text {
		padding: 10rpx 20rpx;
		margin-right: 15rpx;
		border-radius: 20rpx;
		border: solid 1px #dbdbdb;
		font-size: 28rpx;
		color: #999999;
	}

	.oi-header .oi-order-btn .order-btn-blue {
		background-color: #3589f7;
		color: #FFFFFF;
		border-radius: 20rpx;
	}

	.oi-deliver {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}

	.oi-deliver .horseInfo .notice {
		color: #1270e0;
		font-size: 18rpx;
	}

	.oi-deliver .horseInfo .ps-ico {
		font-size: 20rpx;
		margin-left: 10rpx;
	}

	.oi-shop-info {
		display: flex;
		align-items: center;
	}

	.oi-deliver .call,
	.oi-shop-info .call {
		margin-left: auto;
	}

	.oi-goods .oi-shop-info {
		border-bottom: 1rpx dashed #EEEEEE;
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
	}

	.oi-goods .oi-shop-info .oi-shop-logo {
		width: 35rpx;
		height: 35rpx;
		border-radius: 50%;
		margin-top: 8rpx;
	}

	.oi-goods .oi-shop-info .oi-shop-title {
		margin-left: 10rpx;
		font-weight: bold;
	}

	.oi-goods-list {
		margin-top: 15rpx;
	}

	.oi-goods-item {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		margin-bottom: 15rpx;
	}

	.oi-goods-item view {
		line-height: 1.2;
	}

	.oi-goods-item .goodsThumb {
		width: 100rpx;
		height: 100rpx;
		border-radius: 10rpx;
	}

	.oi-goods-item .goods-detail {
		margin-left: 10rpx;
		width: 65%;
	}

	.oi-goods-item .goods-detail .goodsName {
		width: 100%;
		font-weight: bold;
	}

	.oi-goods-item .goods-detail .goodsSpec text {
		font-size: 20rpx;
		margin-right: 15rpx;
		color: #666666;
	}

	.oi-goods-item .goods-detail .goodsNum {
		font-size: 20rpx;
		margin-top: 10rpx;
	}

	.oi-goods-item .goods-price {
		margin-left: auto;
		width: 15%;
		text-align: right;
	}

	.oi-goods-item .goods-price .markprice {
		font-size: 24rpx;
		margin-top: 15rpx;
		text-decoration: line-through;
		color: #666666;
	}

	.oi-fee-item,
	.oi-orther-item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding-bottom: 10rpx;
		font-size: 28rpx;
		width: 100%;
	}

	.oi-fee-item .minusfee {
		color: #FF3333;
	}

	.oi-total {
		text-align: right;
	}

	.oi-tit {

		white-space: nowrap;
	}

	.oi-total .total-prom {
		color: #666666;
		font-size: 24rpx;
	}

	.oi-total .total-price {
		font-weight: bold;
		margin-left: 10rpx;
	}

	.oi-orther-item {
		display: flex;
		justify-content: space-between;
	}

	.oi-orther-item view {
		text-align: right;
	}

	.oi-orther-item .tit {
		color: #666666;
		width: 20%;
	}

	.oi-orther-item .copy-btn {
		margin-left: auto;
	}

	.py-box {
		text-align: center;
		font-size: 28rpx;
	}

	.money-boix {
		text-align: center;
	}

	.pay-money {
		font-size: 45rpx;
		font-weight: bold;
	}

	.pay-btn {
		margin-top: 30upx;
		width: 100%;
		background: #1270e0;
		text-align: center;
		border-radius: 10rpx;
		padding: 10rpx 0;
		color: #FFFFFF;
		height: 100upx;
		font-size: 35upx;


	}

	checkbox-group {
		display: inline !important;
	}


	.os-pay {

		background: #FFFFFF;
		padding: 2%;
		margin-bottom: 15rpx;
		border-radius: 20rpx;
	}

	.os-pay view {
		background-color: #FFFFFF;
	}

	.os-pay .os-pay-item {
		display: flex;
		justify-content: space-between;
		line-height: 70rpx;
	}

	.os-pay .os-pay-item .pay-code {
		text-align: right;

	}

	.os-pay .os-pay-item .pay-note {
		text-align: right;
		color: #999999;
		width: 85%;
		display: flex;
		justify-content: space-between;
		line-height: 75rpx;
	}

	.os-pay .os-pay-item .pay-note .notedetail {
		width: 95%;
		text-align: right;
		line-height: 75rpx;
	}

	.os-pay-ico {
		width: 60rpx;
		height: 60rpx;
		margin-right: 10rpx;
		margin-top: 5rpx;
	}

	.os-pay-left {
		display: flex;
	}

	.os-pay-list {
		background: #FFFFFF;
		width: 100%;
		margin: 0 auto;
		padding: 20rpx;
		border-radius: 15rpx;
		display: flex;
	}

	.os-pay-list .os-pay-item {
		width: 100%;
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 10rpx 0 15rpx 0;
		border-bottom: 1rpx solid #EEEEEE;
	}

	.os-pay-item:last-of-type {
		border: none;
	}
</style>
